<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Administrator
 * Date: 12-8-25
 * Time: 下午1:35
 * To change this template use File | Settings | File Templates.
 */ 
class category extends MY_Controller
{
   public function index()
   {
       $this->load->helper('url');

       $this->load->model('mod_category', 'category');

       $categoryId = $this->uri->rsegment(3, 1);
       $class = config_item('class');
       $category = isset ($class[$categoryId]) ? $class[$categoryId] : $class[1];

       $type = max((int)$this->uri->rsegment(4, 0), 0);

       $Limit = 20;
       $currentPage = max((int)$this->uri->rsegment(5, 1) ,1);
       $offset = ($currentPage - 1) * $Limit;
       //var_dump($categoryId, $type, $currentPage);
        //此处写类别搜索条件
       $searchCondition = "class = {$categoryId} ";
       //$type && $searchCondition['type'] = $type;

       $this->load->model('movie');
       if($type)
       {
           $totalNum = $this->movie->getMovieCountByType($type);
           $tmp = $this->movie->getMovieIDByType($type, $Limit, $offset);
           if($tmp)
           {
               $searchCondition .= " AND id IN (";
               foreach($tmp as $id)
               {
                   $searchCondition .= "{$id['resource_id']}, ";
               }
               $searchCondition .= "0) ";
               $offset = 0;
           }
       }
       else
       {
           $totalNum = $this->movie->getMovieCount($searchCondition);
       }

       $categoryData = array();
       if($totalNum > 0)
       {
           $field = 'id, china_name, director, starring, area, type, version, language, img_addr, public_time, create_time';
           $categoryData  = $this->movie->getMovieList($field, $Limit, $offset, $searchCondition, 'update_time desc');
       }

       $pageHtml = '';
       if($totalNum > $Limit)
       {
           $this->load->library('pagination');
           $config['base_url'] = config_item('i_base_url')."category/{$categoryId}/{$type}";//$type ? "/category/{$categoryId}/{$type}" : "/category/{$categoryId}/{$type}";
           $config['suffix'] = '.html';
           $config['total_rows'] = $totalNum;
           $config['per_page'] = $Limit;
           $config['num_links'] = 5;
           $config['uri_segment'] = 5;
           $config['first_link'] = '首页';
           $config['last_link'] = '尾页';
           $config['use_page_numbers'] = TRUE;
           $config['cur_tag_open'] = '<a id="cur">';
           $config['cur_tag_close'] = '</a>';
           $config['prev_link'] = '上一页';
           $config['next_link'] = '下一页';
           $config['first_url'] = $config['i_base_url'].'/1.html';
           $this->pagination->initialize($config);
           $pageHtml = $this->pagination->create_links();
       }
       $tmp = $this->category->get_category(true);
       $children = isset($tmp[$categoryId]['children']) ? $tmp[$categoryId]['children']:array();
       //echo '<pre>'; print_r($children);
       //$categoryData['id'] = 10000000;
       //$categoryData['class'] = $categoryId;
       $vData = array(
           'title' => $category.'列表  第 '.$currentPage.' 页, 分类组合筛选 电影1010',
           'movies'  => $categoryData,
           'data' => array('id' => 9999999, 'class' => $categoryId),
           'page_html' => $pageHtml,
           'class_name' => $category,
           'categoryId'=>$categoryId,
           //'category'=>$this->category->get_category(true)
           'children' => $children,
           'type' => $type,
           "keywords" => '全网最全的资源库 迅雷下载 电驴下载 电影1010',
           'description' => '电影1010 全网最全的资源库,为广大电影爱好者提供精选电影迅雷下载以及观看,本站支持迅雷看看电影点播以及最新的百度影音播放。',
       );
       $this->load->view('list', $vData);
       $this->saveHTML(array($categoryId, $type), $currentPage);
   }

   private function saveHTML($condition, $page)
   {
       $class = implode('/', $condition);
       $this->load->helper('directory');
       $path = FCPATH."category/{$class}/";
       recursiveMkdirDirectory($path);
       file_put_contents($path."{$page}.html", $this->formatHTML(), LOCK_EX);
   }
}
